package com.gewu.pm.ui.fragment.clockin;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.TitleBarFragment;
import com.gewu.pm.bean.AttendanceRecordBean;
import com.gewu.pm.ui.activity.clockin.ClockInDetailActivity;
import com.gewu.pm.ui.activity.clockin.ClockInRecordActivity;
import com.gewu.pm.ui.activity.filter.FilterClockInRecordActivity;
import com.gewu.pm.ui.adapter.MyAttendanceAdapter;
import com.gewu.pm.ui.dialog.FillDescriptionDialog;
import com.gewu.pm.ui.popup.SelectTimePopup;
import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 我的打卡记录Fragment
 */
public final class MyClockInFragment extends TitleBarFragment<ClockInRecordActivity>
        implements BaseAdapter.OnItemClickListener, MyAttendanceAdapter.OnItemActionListener {

    private TextView tvFilterType;
    private ImageView ivFilter;
    private SmartRefreshLayout srlRefresh;
    private RecyclerView rvList;

    private MyAttendanceAdapter mAdapter;
    private SelectTimePopup.Builder mFilterPopup;
    private int mCurrentFilterType = 0; // 0=全部，1=迟到，2=早退，3=未打卡

    public static MyClockInFragment newInstance() {
        return new MyClockInFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.my_clock_in_fragment;
    }

    @Override
    protected void initView() {
        tvFilterType = findViewById(R.id.tv_filter_type);
        ivFilter = findViewById(R.id.iv_filter);
        srlRefresh = findViewById(R.id.srl_refresh);
        rvList = findViewById(R.id.rv_list);

        mAdapter = new MyAttendanceAdapter(getAttachActivity());
        // ⚠️ 必须在 setAdapter 之前设置监听器
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemActionListener(this);
        rvList.setAdapter(mAdapter);

        srlRefresh.setOnRefreshListener(refreshLayout -> {
            loadData();
            refreshLayout.finishRefresh(1000);
        });

        setOnClickListener(tvFilterType, ivFilter);
    }

    @Override
    protected void initData() {
        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        // 模拟数据
        List<AttendanceRecordBean> list = new ArrayList<>();
        
        // 第一条：迟到
        AttendanceRecordBean bean1 = new AttendanceRecordBean();
        bean1.setId("1");
        bean1.setDate("2025-01-01");
        bean1.setSignInTime("07:00:01");
        bean1.setSignOutTime("16:30:01");
        bean1.setCheckStatus(2); // 迟到
        bean1.setSignOffStatus(1); // 正常签退
        list.add(bean1);
        
        // 第二条：正常
        AttendanceRecordBean bean2 = new AttendanceRecordBean();
        bean2.setId("2");
        bean2.setDate("2025-01-01");
        bean2.setSignInTime("06:50:00");
        bean2.setSignOutTime("18:00:00");
        bean2.setCheckStatus(1); // 正常签到
        bean2.setSignOffStatus(1); // 正常签退
        list.add(bean2);
        
        // 第三条：未打卡 + 早退
        AttendanceRecordBean bean3 = new AttendanceRecordBean();
        bean3.setId("3");
        bean3.setDate("2025-01-01");
        bean3.setSignInTime("");
        bean3.setSignOutTime("16:30:01");
        bean3.setCheckStatus(0); // 未打卡
        bean3.setSignOffStatus(2); // 早退
        list.add(bean3);
        
        mAdapter.setData(list);
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.tv_filter_type) {
            showFilterPopup();
        } else if (viewId == R.id.iv_filter) {
            FilterClockInRecordActivity.start(getAttachActivity());
        }
    }

    /**
     * 显示筛选弹窗
     */
    private void showFilterPopup() {
        if (mFilterPopup == null) {
            List<String> filterList = Arrays.asList(
                    getString(R.string.all),
                    getString(R.string.late),
                    getString(R.string.early_leave),
                    getString(R.string.not_clocked_in)
            );
            
            mFilterPopup = new SelectTimePopup.Builder(getAttachActivity())
                    .setList(filterList)
                    .setListener((popup, data) -> {
                        tvFilterType.setText(data);
                        // TODO: 根据筛选类型重新加载数据
                        toast("选择了：" + data);
                    });
        }
        mFilterPopup.showAsDropDown(tvFilterType);
    }

    /**
     * {@link BaseAdapter.OnItemClickListener}
     */
    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        AttendanceRecordBean bean = mAdapter.getItem(position);
        ClockInDetailActivity.start(getAttachActivity(), bean);
    }
    /**
     * 填写说明
     * @param isSignIn true=签到，false=签退
     */
    @Override
    public void onFillDescription(int position, AttendanceRecordBean bean, boolean isSignIn) {
        new FillDescriptionDialog.Builder(getAttachActivity())
                .setListener(new FillDescriptionDialog.OnListener() {
                    @Override
                    public void onConfirm(BaseDialog dialog, String description) {
                        toast("填写说明：" + description);
                        // TODO: 提交说明到服务器
                    }
                })
                .show();
    }
    /**
     * 查看图片
     * @param isSignIn true=签到，false=签退
     */
    @Override
    public void onViewPhoto(int position, AttendanceRecordBean bean, boolean isSignIn) {
        toast("查看图片");
        // TODO: 跳转到图片预览页面
    }
}
